package fnord.jovian;

import java.awt.Graphics2D;
import java.awt.Point;
import java.util.Vector;

import fnord.jovian.tango.*;

public class TheField implements TheObject{

	private static final double _xPad = 100, _yPad = 100, _cardPad = 10;
	private double _xOff = 0, _yOff = 0, _w = 0, _h = 0;
	private Vector<TheObject> _grid;
	private TheDeck _deck;
	private TheBoard _board;
	private boolean _visible = false, _mini = false;

	public TheField(TheBoard board, boolean mini) {
		_board = board;
		_deck = new TheDeck(board,mini);
		_grid = new Vector<TheObject>();
		_mini = mini;
		if (_mini) {
			_visible = true;
		}
	}

	public void addCard(TheCard card) {
		_grid.add(new TheCardTree(card,_board,true));
	}

	private void reOrient() {
		if (!_mini) {
			int xnum = (int)Math.floor((_board.getWidth() - _xPad*2.0 + _cardPad)/(TheTangoConstants.CARDWIDTH + _cardPad));
			_deck.setLocation(_xOff + (_board.getWidth() - _deck.getWidth())/2.0 + _xOff, _yPad + _yOff);
			double y = _yPad + _deck.getHeight() - TheTangoConstants.CARDHEIGHT;
			for (int i = 0; i < _grid.size(); i++) {
				if ((i % xnum) == 0)
					y += TheTangoConstants.CARDHEIGHT + _cardPad;
				_grid.get(i).setLocation(_xPad + (TheTangoConstants.CARDWIDTH + _cardPad)*(i % xnum) + _xOff,y + _yOff);
			}
		}
		else {
			int xnum = (int)Math.floor((_w - _xPad*2.0 + 3)/(TheTangoConstants.MINICARDWIDTH + 3));
			_deck.setLocation(_xOff + (_w - _deck.getWidth())/2.0, _yOff);
			double y = _deck.getHeight() - TheTangoConstants.MINICARDHEIGHT;
			for (int i = 0; i < _grid.size(); i++) {
				if ((i % xnum) == 0)
					y += TheTangoConstants.MINICARDHEIGHT + 3;
				_grid.get(i).setLocation((TheTangoConstants.MINICARDWIDTH + 3)*(i % xnum) + _xOff,y + _yOff);
			}
		}
	}
	
	public boolean getMini() {
		return _mini;
	}
	
	public void setMini(boolean mini) {
		_mini = mini;
	}

	public void paint(Graphics2D g) {
		reOrient();
		if (_visible) {
			_deck.paint(g);
			for (int i = _grid.size()-1; i >= 0; i--) {
				_grid.get(i).paint(g);
			}
		}
	}

	public void move(double x, double y) {
		_xOff = x;
		_yOff = y;
	}

	public void shift(double x, double y) {
		_xOff += x;
		_yOff += y;
	}

	public void setActive(boolean visible) {
		_visible = visible;
	}


	public boolean contains(Point p) {
		boolean contains = false;
		for (int i = 0; i < _grid.size(); i++) {
			if (_grid.get(i).contains(p))
				contains = true;
		}
		return contains;
	}

	public boolean containsClick(Point p) {
		boolean contains = false;
		for (int i = 0; i < _grid.size(); i++) {
			if (_grid.get(i).containsClick(p))
				contains = true;
		}
		return contains;
	}

	public boolean containsUp(Point p) {
		boolean contains = false;
		for (int i = 0; i < _grid.size(); i++) {
			if (_grid.get(i).containsUp(p))
				contains = true;
		}
		return contains;
	}

	public boolean containsDrag(Point p) {
		boolean contains = false;
		for (int i = 0; i < _grid.size(); i++) {
			if (_grid.get(i).containsDrag(p))
				contains = true;
		}
		return contains;
	}

	public double getX() {
		return _xOff;
	}

	public double getY() {
		return _yOff;
	}

	@Override
	public double getCenterX() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public double getCenterY() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void setLocation(double x, double y) {
		_xOff = x;
		_yOff = y;
	}

	public double getWidth() {
		return _w;
	}

	public double getHeight() {
		return _h;
	}

	public void setSize(double w, double h) {
		_w = w;
		_h = h;
	}

}
